import React from 'react';
import {
    Button, Input, Form, message} from 'antd';
import * as Request from '@/services/Request';

const apiURL = "/api/services/app/Warehse/";
class WarehseEntry extends React.Component<any, any> {
    constructor(props: any) {
        super(props);
        this.state = {
            loading: false,
            dataSource: [],
        }
    }
    formRef: any = React.createRef();
    componentDidMount() {
        this.fetchDatas();
    }
    onFinish = (values: any) => {
        var model = values.model;
        model.id = this.props.id;
        this.setState({ loading: true });
        var act = "Modify";
        if (model.id == 0 || !model.id) {
            act = "New";
        }
        model.zip = "0";
        model.city = "0";
        model.state = "0";
        model.faxNum = "0";
        model.address = "0";
        model.country = "0";
        model.phoneNum = "0";
        Request.post(apiURL + act, model).then((response) => {
            if (response) {
                if (response.success) {
                    if (response.result.success) {
                        message.success('保存成功！');
                        this.props.fetchData();
                    }
                    else {
                        message.error(response.result.message);
                    }
                }
                else {
                    message.error(response.error.message);
                }
                this.setState({ loading: false });
            }
        });
    }
    fetchDatas = () => {
        const { id } = this.props;
        if (id > 0) {
            Request.get(apiURL + 'Get?Id=' + id).then((response) => {
                var model = response?.result;
                this.formRef.current.setFieldsValue({ model });
            });
        }
    }
    render() {
        const layout = {
            labelCol: { span: 5 },
            wrapperCol: { span: 19 },
        };
        return (
            <Form {...layout} ref={this.formRef} onFinish={this.onFinish} >
                <Form.Item name={['model', 'warehouseCode']} label="仓库代码" rules={[{ required: true }]}>
                    <Input />
                </Form.Item>
                <Form.Item name={['model', 'description']} label="描述" rules={[{ required: true }]}>
                    <Input />
                </Form.Item>
                <Form.Item name={['model', 'warehouseType']} label="仓库类型">
                    <Input />
                </Form.Item>
                <Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 5 }}>
                    <Button type="primary" htmlType="submit">保存</Button>
                </Form.Item>
            </Form>
        )
    }
}
export default WarehseEntry